﻿using System;
using System.ComponentModel.Composition;
using Cubicle.Core.Interfaces;
using Cubicle.Core.Shared;

namespace Cubicle.Core.Processors.Parameter
{
    [Export("Cubicle.Processors.Parameter.TypeStringParameterResolver", typeof(IProcessor<ParameterRequest>))]
    [ExportMetadata("Name", "TypeStringParameterResolver")]
    [ExportMetadata("Description", "Resolves a type fullName to System.Type")]
    public class TypeStringParameterResolver : ConditionalProcessor<ParameterRequest>
    {
        private readonly Type _stringType = typeof(string);

        #region Overrides of Processor<ParameterRequest>

        public override bool Condition(ParameterRequest request)
        {
            return request.SourceType == _stringType 
                   && request.TargetType == typeof(Type);
        }

        public override ParameterRequest ConditionalProcess(ParameterRequest request)
        {
            var strValue = (string)request.Source;
            var result = TypeCache.GetType(strValue);
            request.Complete(result);
            return request;
        }

        #endregion
    }
}